package android.team.app.network;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.os.AsyncTask;

public class AsyncHttpBase extends AsyncTask<String, Integer, String>{
	
	public static final int STATUS_NETWORK_OK = 0;
	public static final int STATUS_NETWORK_OFF = 1;
	public static final int STATUS_NETWORK_ERROR = 2;
	public static final int STATUS_NETWORK_TIMEOUT = 3;
	public static final int STATUS_NETWORK_EXCEIPTION = 4;
	
	protected Context mContext;
	protected AsyncHttpResponseListener listener;
	protected HttpResponse response;
	protected List<NameValuePair> parameters;
	protected int statusCode;
	
	/**
	 * xay dung phuong thuc khoi tao
	 * @param context
	 * @param listener
	 * @param params
	 */
	public  AsyncHttpBase(Context context, AsyncHttpResponseListener listener, List<NameValuePair> params) {
		this.mContext = context;
		this.listener = listener;
		this.parameters = params;
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		//duoc goi truoc khi thuc hien qua trinh load data
		listener.before();
	}
	
	@Override
	protected String doInBackground(String... params) {
		// duoc goi de chay ngam va thuc hien lay du lieu tren server
		//check neu mang co thi thuc hien gui request
		boolean isNetwork = true;
		if(isNetwork){
			return request(params[0]);
		}else{
			statusCode = STATUS_NETWORK_OFF;
			return null;
		}
		
	}
	//xay dung phuong thuc thuc hien lay du lieu va tra ve kieu string
	protected String request(String url) {
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		//duoc goi khi qua trinh lay du lieu ket thuc
		listener.after(statusCode, response);
	}

	//xay dung phuong thuc tao ra httpClient
	protected HttpClient CreateHttpClient(HttpParams params) {
		return new DefaultHttpClient(params);
	}
}
